package cn._51doit.flink.day02.transformations;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.operators.StreamFilter;

/**
 * 使用transform方法实现与filter类似的功能
 */
public class FilterDemo3 {

    public static void main(String[] args) throws Exception {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //spark
        //hive
        //hadoop
        DataStreamSource<String> lines = env.socketTextStream("localhost", 8888);


        //输出以h开头的数据
        FilterFunction<String> filterFunction = new FilterFunction<String>() {
            @Override
            public boolean filter(String line) throws Exception {
                return line.startsWith("h");
            }
        };

        SingleOutputStreamOperator<String> filtered = lines.transform("MyFilter", TypeInformation.of(String.class), new StreamFilter<>(filterFunction));

        filtered.print();

        env.execute();
    }

}
